<template>
  <body id="poster">
    <div id="title">电子书阅览系统</div>
    <el-form class="login-container" label-position="left"
             label-width="0px">
      <h3 class="login_title">系统登录</h3>
      <el-form-item>
        <el-input type="text" v-model="loginForm.username" placeholder="用户名"/>
      </el-form-item>
      <el-form-item>
          <el-input type="password" v-model="loginForm.password" placeholder="密码"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="select"  placeholder="身份">
          <el-option label="管理员" value="管理员"></el-option>
          <el-option label="游客" value="游客"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 40%;background: #505458;border: none" v-on:click="login">登录</el-button>
        <el-button type="primary" style="width: 40%;background: #505458;border: none" v-on:click="save">注册</el-button>
      </el-form-item>
    </el-form>
  </body>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      select: '',
      responseResult: []
    }
  },
  methods: {
    login () {
      this.$axios
        .post('/login', {
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        .then(successResponse => {
          if (successResponse.data.code === 200 && this.select === '管理员') {
            this.$router.replace({path: '/manage'})
            console.log(this.select)
          } else if (successResponse.data.code === 200 && this.select === '游客') {
            this.$router.replace({path: '/book'})
            console.log(this.select)
          } else {
            alert("账号密码错误！")
          }
        })
        .catch(failResponse => {

        })
    },
    save () {
      this.$axios
        .post('/save', {
          username: this.loginForm.username,
          password: this.loginForm.password,
          type: this.select
        })
        .then(successResponse => {
          if (successResponse.data.code === 200) {
            alert('注册成功')
          } else {
            alert('注册失败')
          }
        })
    }
  }
}
</script>

<style scoped>
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 250px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  #poster {
    background:url("../assets/bac_01.jpg") no-repeat;
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: 10px;
  }
  .el-select{
    width: 350px;
    height: 40px;
  }
  #title{
    position: relative;
    top: 200px;
    font-family: Calibri;
    font-size: 50px;
    color: #eaeaea;
  }
</style>
